package com.seaengine.actor.anibmp
{
	import com.seaengine.GameConfig;
	import com.seaengine.actor.anibmp.characterbmp.CharacterBmpMgr;
	import com.seaengine.actor.anibmp.effectbmp.EffectBmpMgr;
	import com.seaengine.actor.anibmp.weaponbmp.WeaponBmpMgr;
	import com.seaengine.proxy.CharacterKindConfigProxy;
	import com.seaengine.resourcemanager.ResSystem;
	
	import flash.utils.Dictionary;
	
	public class AnimationBMPMgr
	{
		//单件
		private static var s_Instance:AnimationBMPMgr;
		private static var m_bSingleton:Boolean = true;
		
		private var m_Res_Map:Dictionary;		
		private var m_callbackList:Dictionary;
		private var m_paramList:Dictionary;
		
		public function AnimationBMPMgr()
		{
			if(m_bSingleton)
			{
				throw new Error("只能用getInstance()来获取实例");
			}			
			reset();
		}
		
		public static function getInstance():AnimationBMPMgr
		{
			if(!s_Instance)
			{
				m_bSingleton = false;
				s_Instance = new AnimationBMPMgr();
				m_bSingleton = true;				
			}
			return s_Instance;
		}
		
		private function reset():void
		{
			m_Res_Map = new Dictionary();
			m_callbackList = new Dictionary();
			m_paramList = new Dictionary();
		}
		
		private function onXMLLoaded(_fileName:String,obj:Object):void
		{
			var _xml:XML = obj as XML;
			var typeID:int = CharacterKindConfigProxy.getInstance().getTypeIDByXMLName(_fileName);
			var strType:String = CharacterKindConfigProxy.getInstance().getKindByID( typeID );
			if(strType == CharacterKindConfigProxy.TYPE_CHARACTER )
			{
				m_Res_Map[typeID] =  new CharacterBmpMgr(typeID,_xml,onMgrLoaded);
				 
			}else if(strType == CharacterKindConfigProxy.TYPE_EFFECT)
			{
				m_Res_Map[typeID] =  new EffectBmpMgr(typeID,_xml,onMgrLoaded);
				
			}else if(strType == CharacterKindConfigProxy.TYPE_WEAPON)
			{
				m_Res_Map[typeID] =  new WeaponBmpMgr(typeID,_xml,onMgrLoaded);
			}
		}
		
		public function getBMPManager(_typeID:int):BaseBmpMgr
		{
			return m_Res_Map[_typeID];		
		}
		
		private function onMgrLoaded(_mgr:BaseBmpMgr):void
		{
			if(!m_Res_Map[_mgr.m_Id])
			{
				m_Res_Map[_mgr.m_Id] = _mgr;
			}
			
			var _callbakcList:Array = m_callbackList[_mgr.m_Id];
			var _paramList:Array = m_paramList[_mgr.m_Id];
			if(_callbakcList != null)
			{
				for(var i:int = 0 ; i < _callbakcList.length; i++)
				{
					getAnimationBmpInfo( _mgr.m_Id,_paramList[i],_callbakcList[i]);
				}
			}
			m_callbackList[_mgr.m_Id] = null;
			m_paramList[_mgr.m_Id] = null;	
		}
		
		public function unloadAnimationBmpInfo(_type:int):void
		{	
			m_Res_Map[_type]=null	
		}	
		
		public function getAnimationBmpInfo(_type:int,_animationId:int,_onCallbackFn:Function):void
		{		
			if(_type==-1)
				throw new Error("不能加载这些类型");	
			var mgr:BaseBmpMgr = m_Res_Map[_type];
			
			if( mgr != null && mgr.m_bLoaded )
			{
				if(_onCallbackFn != null )
				{
				
				_onCallbackFn( mgr.getNodeInfo(_animationId ));
				}
				return;
			}
			
			var xmlName:String = CharacterKindConfigProxy.getInstance().getXMLNameByTypeID(_type);
			
			var _callbackListAr:Array = m_callbackList[_type];
			var _paramList:Array = m_paramList[_type];
			if(	_callbackListAr!= null)
			{				
				_callbackListAr.push( _onCallbackFn );
				_paramList.push(_animationId);
			}
			else
			{
				_callbackListAr = new Array();
				_paramList = new Array();
				_callbackListAr.push( _onCallbackFn );
				_paramList.push( _animationId );
				m_callbackList[_type] = _callbackListAr;
				m_paramList[_type] = _paramList;
				ResSystem.getInstance().getRes( GameConfig.m_ResConfig + xmlName,onXMLLoaded );
			}	
		}	
	}
}